﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;

using com.vs.General;
using com.vs.DataAccess;
using com.vs.DataAccess.DO;

namespace com.vs.Task.BL
{
    public class TaskManager
    {
        #region GetTaskHdrsByQFCri
        public DataTable GetTaskHdrsByQFCri(Session sess, List<KeyValuePair<string, KeyValuePair<string, object>>> dcParam, string stOrderBy, ref string stError)
        {
            DataHelper helper = null;
            StringBuilder sbSQL = null;
            int i = 0;
            IDataParameter[] pars = null;

            try
            {
                sbSQL = new StringBuilder(" SELECT DISTINCT TH.* FROM TASK_HEADER TH LEFT JOIN TASK_DETAIL TD ON TH.INTERNAL_TASK_NUM = TD.INTERNAL_TASK_NUM WHERE 1=1 ");
                helper = new DataHelper(sess);
                pars = new IDataParameter[dcParam.Count];
                foreach (KeyValuePair<string, KeyValuePair<string, object>> de in dcParam)
                {
                    sbSQL.Append(" AND ").Append(de.Key.ToString()).Append(de.Value.Key).Append("@Value").Append(i);
                    pars[i] = new SqlParameter("@Value" + i, de.Value.Value);
                    i++;
                }

                if (!StringManager.IsEmpty(stOrderBy))
                {
                    sbSQL.Append(stOrderBy);
                }
                return helper.GetTable(CommandType.Text, sbSQL.ToString(), pars);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                stError = MessageManager.MSG_GEN_000 + e.Message;
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }
        #endregion

        #region GetNextFromTask
        /// <summary>
        /// 获取可以拣货的任务
        /// </summary>
        /// <param name="sess"></param>
        /// <param name="stUser"></param>
        /// <returns></returns>
        public TaskHeaderDO GetNextFromTask(Session sess, TaskPreferenceDO prefDO, ref string stError)
        {
            DataHelper helper = null;
            DataRow drTask = null;
            string stSQL = "TSK_RNextFromTask";

            try
            {
                helper = new DataHelper(sess);
                drTask = helper.GetRow(CommandType.StoredProcedure, stSQL, new IDataParameter[]{
                        DataHelper.BuildParameter(sess, "@PREFERENCE_NAME", prefDO.PREFERENCE_NAME),
                        DataHelper.BuildParameter(sess, "@USER", sess.User)
                    });

                if (drTask != null)
                    return TaskHeaderDO.GetModel(drTask);
                return null;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                stError = MessageManager.MSG_GEN_000 + e.Message;
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }
        #endregion

        #region GetNextFromTaskDetailGroup
        /// <summary>
        /// 获取下一组可以执行的From的任务明细
        /// </summary>
        /// <param name="sess"></param>
        /// <param name="stWorkUnit"></param>
        /// <returns></returns>
        public List<TaskDetailDO> GetNextFromTaskDetailGroup(Session sess, int iIntTaskNum, ref string stError)
        {
            DataHelper helper = null;
            DataTable dtTask = null;
            string stSQL = "TSK_RNextFromTaskDetailGroup";

            try
            {
                helper = new DataHelper(sess);
                dtTask = helper.GetTable(CommandType.StoredProcedure, stSQL, new IDataParameter[]{
                        DataHelper.BuildParameter(sess, "@INTERNAL_TASK_NUM", iIntTaskNum)
                    });

                if (!DataManager.IsEmpty(dtTask))
                    return TaskDetailDO.LoadListFromDataView(dtTask.DefaultView);
                else
                    return null;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                stError = MessageManager.MSG_GEN_000 + e.Message;
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }
        #endregion

        #region GetNextToTaskDetailGroup
        /// <summary>
        /// 获取下一组可以执行的To的任务明细
        /// </summary>
        /// <param name="sess"></param>
        /// <param name="stWorkUnit"></param>
        /// <returns></returns>
        public List<TaskDetailDO> GetNextToTaskDetailGroup(Session sess, int iIntTaskNum, ref string stError)
        {
            DataHelper helper = null;
            DataTable dtTask = null;
            string stSQL = "TSK_RNextToTaskDetailGroup";

            try
            {
                helper = new DataHelper(sess);
                dtTask = helper.GetTable(CommandType.StoredProcedure, stSQL, new IDataParameter[]{
                        DataHelper.BuildParameter(sess, "@INTERNAL_TASK_NUM", iIntTaskNum)
                    });

                if (!DataManager.IsEmpty(dtTask))
                    return TaskDetailDO.LoadListFromDataView(dtTask.DefaultView);
                else
                    return null;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                stError = MessageManager.MSG_GEN_000 + e.Message;
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }
        #endregion

        #region GetAvailableTask
        public TaskHeaderDO GetAvailableTask(Session sess, string stTaskID, TaskPreferenceDO prefDO, ref string stError)
        {
            DataHelper helper = null;
            StringBuilder sbSQL = null;
            DataTable dtTask = null;

            try
            {
                sbSQL = new StringBuilder(512)
                    .Append(" SELECT * FROM TASK_HEADER WHERE TASK_ID = @TASK_ID ")
                    .Append(" AND CONDITION <> 'CLOSED' ")
                    .Append(" AND (ASSIGNED_USER IS NULL OR ASSIGNED_USER = @ASSIGNED_USER) ");
                //任务类型限制
                if (!StringManager.IsEmpty(prefDO.TASK_TYPE))
                {
                    sbSQL.Append(" AND TASK_TYPE = @TASK_TYPE ");
                }

                //工作区限制
                if (!StringManager.IsEmpty(prefDO.ZONE))
                {
                    sbSQL.Append(" AND ZONE = @ZONE ");
                }

                helper = new DataHelper(sess);
                dtTask = helper.GetTable(CommandType.Text, sbSQL.ToString(), new IDataParameter[]{
                            DataHelper.BuildParameter(sess, "@TASK_ID", stTaskID),
                            DataHelper.BuildParameter(sess, "@ASSIGNED_USER", sess.User),
                            DataHelper.BuildParameter(sess, "@TASK_TYPE", prefDO.TASK_TYPE),
                            DataHelper.BuildParameter(sess, "@ZONE", prefDO.ZONE)});

                if (DataManager.IsEmpty(dtTask))
                    return null;
                return TaskHeaderDO.GetModel(dtTask.Rows[0]);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                stError = MessageManager.MSG_GEN_000 + e.Message;
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }
        #endregion
    }
}
